using System;
using System.Collections.Generic;
using System.Text;

using Weazel.Microblaze.Simulator.Engine;

namespace Weazel.Microblaze.Simulator.Components.Microblaze.Instructions
{
    public class Swi : MemoryAccessInstruction
    {
        public override int CycleCount
        {
            get { return 1; }
        }

        public Swi()
            : base("swi")
        {
        }

        public override void Execute(MicroblazeComponent MicroblazeComponent)
        {

            uint address = (uint)(
              MicroblazeComponent.ReadRegister(SourceRegisterA) + GetImmediateValue(MicroblazeComponent)
            );

            uint maskedAddress = address & 0xFFFFFFFC;

            uint rd = MicroblazeComponent.ReadRegister(DestinationRegister);

            WriteWordTransaction transaction = new WriteWordTransaction(MicroblazeComponent, maskedAddress, rd);
            MicroblazeComponent.BeginDataTransaction(transaction);

            while (transaction.Status != Transaction.TransactionStatus.Complete)
                MicroblazeComponent.WaitOneCycle();
        }

        public override string ToString()
        {
            return string.Format("swi r{0}, r{1}, {2}",
              DestinationRegister,
              SourceRegisterA,
              ImmediateValue
            );
        }
    }
}
